package com.it.schoolbookshop_back.mapper;

import com.it.schoolbookshop_back.entities.po.Book;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface FavoriteMapper {

    /**
     * 添加收藏
     * 
     * @param userId 用户ID
     * @param bookId 书籍ID
     * @return 影响行数
     */
    @Insert("INSERT INTO favorites(user_id, book_id) VALUES(#{userId}, #{bookId})")
    int addFavorite(@Param("userId") Integer userId, @Param("bookId") Integer bookId);
    
    /**
     * 取消收藏
     * 
     * @param userId 用户ID
     * @param bookId 书籍ID
     * @return 影响行数
     */
    @Delete("DELETE FROM favorites WHERE user_id = #{userId} AND book_id = #{bookId}")
    int cancelFavorite(@Param("userId") Integer userId, @Param("bookId") Integer bookId);
    
    /**
     * 检查用户是否已收藏此书
     * 
     * @param userId 用户ID
     * @param bookId 书籍ID
     * @return 如果已收藏返回记录ID，否则返回null
     */
    @Select("SELECT id FROM favorites WHERE user_id = #{userId} AND book_id = #{bookId}")
    Integer checkFavorite(@Param("userId") Integer userId, @Param("bookId") Integer bookId);
    
    /**
     * 检查书籍是否存在，不考虑状态
     * 
     * @param bookId 书籍ID
     * @return 如果存在返回true，否则返回false
     */
    @Select("SELECT COUNT(1) FROM books WHERE id = #{bookId}")
    boolean checkBookExists(@Param("bookId") Integer bookId);
    
    /**
     * 检查书籍是否可收藏（存在且状态为active）
     * 
     * @param bookId 书籍ID
     * @return 如果可收藏返回true，否则返回false
     */
    @Select("SELECT COUNT(1) FROM books WHERE id = #{bookId} AND status = 'active'")
    boolean checkBookFavoritable(@Param("bookId") Integer bookId);
    
    /**
     * 获取用户收藏的书籍列表
     * 
     * @param userId 用户ID
     * @return 书籍列表
     */
    @Select("SELECT b.* FROM books b " +
            "INNER JOIN favorites f ON b.id = f.book_id " +
            "WHERE f.user_id = #{userId} " +
            "ORDER BY f.created_at DESC")
    List<Book> getFavoriteBooks(@Param("userId") Integer userId);
    
    /**
     * 增加用户收藏数量
     * 
     * @param userId 用户ID
     * @return 影响行数
     */
    @Update("UPDATE users SET favoriteCount = favoriteCount + 1 WHERE id = #{userId}")
    int increaseFavoriteCount(@Param("userId") Integer userId);
    
    /**
     * 减少用户收藏数量
     * 
     * @param userId 用户ID
     * @return 影响行数
     */
    @Update("UPDATE users SET favoriteCount = favoriteCount - 1 WHERE id = #{userId} AND favoriteCount > 0")
    int decreaseFavoriteCount(@Param("userId") Integer userId);
    
    /**
     * 根据书籍ID删除所有收藏记录
     * 当删除书籍时，需要同时删除所有用户对该书籍的收藏记录
     * 
     * @param bookId 书籍ID
     * @return 影响行数（即删除的收藏记录数）
     */
    @Delete("DELETE FROM favorites WHERE book_id = #{bookId}")
    int deleteFavoritesByBookId(@Param("bookId") Integer bookId);
    
    /**
     * 根据书籍ID获取所有收藏该书籍的用户ID
     * 用于在删除书籍时更新用户的收藏计数
     * 
     * @param bookId 书籍ID
     * @return 用户ID列表
     */
    @Select("SELECT user_id FROM favorites WHERE book_id = #{bookId}")
    List<Integer> getUsersByFavoriteBook(@Param("bookId") Integer bookId);
} 